package com.student.dao.impl;

import com.student.dao.BaseDao;
import com.student.dao.StudentDao;
import com.student.pojo.StuInfo;
import com.student.util.DBHelper;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生接口实现类
 */
public class StudentDaoImpl extends BaseDao implements StudentDao {

    public StudentDaoImpl(Connection con) {
        super(con);
    }

    @Override
    public Integer addStudent(StuInfo student) {
        Integer count=0;
        StringBuilder sql=new StringBuilder();
        sql.append("INSERT INTO `stu_info`(`name`,`password`,`sex`,`age`,`weight`,`height`,`role` ");
        sql.append(" VALUES(?,?,?,?,?,?,? )");
        try {
            count = this.executeUpdate(sql.toString(),new Object[]{
                    student.getName(),student.getPassword(),
                    student.getSex(), student.getAge(),student.getWeight(),
                    student.getHeight(),student.getRole()
            });
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public Integer delStudent(Integer studentId) {
        Integer count=0;
        String sql="DELETE FROM `stu_info` WHERE id=?";
        try {
            count=this.executeUpdate(sql,new Object[]{studentId});
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public Integer updateStudent(StuInfo student) {
        int count=0;
        StringBuilder sql=new StringBuilder();
        sql.append("UPDATE `stu_info` SET `name`=?,`password`=?,");
        sql.append("`sex`=?,`age`=?,`weight`=?,`height`=?,`role`=?  WHERE id=?");
        try {
            count=this.executeUpdate(sql.toString(),new Object[]{
                    student.getName(),student.getPassword(),
                    student.getSex(), student.getAge(),student.getWeight(),
                    student.getHeight(),student.getRole() ,student.getId()
            });
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public List<StuInfo> seeStudent(Integer id,String name) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT t1.*   FROM `stu_info` t1   WHERE 1=1");
        ResultSet rs = null;
        StuInfo stuInfo = null;
        List<StuInfo> list = new ArrayList<>();
        try {
            if (name!=null && !name.equals("")){
                sql.append(" AND t1.`name` LIKE '%"+name+"%'");
                rs=this.excuteQuery(sql.toString(),null);
            }
            else if (id!=null && !id.equals(0)){
                sql.append(" AND t1.`id`=?");
                rs=this.excuteQuery(sql.toString(),new Object[]{id});
            }
            else {
                rs=this.excuteQuery(sql.toString(),null);
            }
            while (rs.next()){
                stuInfo = new StuInfo();
                stuInfo.setId(rs.getInt("id"));
                stuInfo.setName(rs.getString("name"));
                stuInfo.setPassword(rs.getString("password"));
                stuInfo.setSex(rs.getString("sex"));
                stuInfo.setAge(rs.getInt("age"));
                stuInfo.setWeight(rs.getDouble("weight"));
                stuInfo.setHeight(rs.getDouble("height"));
                stuInfo.setRole(rs.getString("role"));
                list.add(stuInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBHelper.close(rs,null,null);
        }
        return list;
    }

    @Override
    public StuInfo studentLogin(String name, String password, String role) {
        String sql = "SELECT * FROM `stu_info` WHERE `name`=? AND `password`=? AND `role`=?";
        ResultSet rs = null;
        StuInfo stuInfo = null;
        try {
            rs=this.excuteQuery(sql,new Object[]{name,password,role});
            while (rs.next()){
                stuInfo = new StuInfo();
                stuInfo.setId(rs.getInt("id"));
                stuInfo.setName(rs.getString("name"));
                stuInfo.setPassword(rs.getString("password"));
                stuInfo.setSex(rs.getString("sex"));
                stuInfo.setAge(rs.getInt("age"));
                stuInfo.setWeight(rs.getDouble("weight"));
                stuInfo.setHeight(rs.getDouble("height"));
                stuInfo.setRole(rs.getString("role"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBHelper.close(rs,null,null);
        }
        return stuInfo;
    }
}
